package game;
import java.util.ArrayList;
import fields.*;

public class PropertyManager {

	private ArrayList<Ownable> properties;
	
	public PropertyManager() {
		properties = new ArrayList<Ownable>();
	}
	
	public void addProperty(Ownable newProperty) {
		properties.add(newProperty);
	}
	
	public void removeProperty(int index) {
		properties.remove(index);
	}
	
	public Ownable getProperty(int index) {
		return properties.get(index);
	}
	
	public int numberOfProperties() {
		return properties.size();
	}
	
	public boolean containsProperty(Ownable field) {
		return properties.contains(field);
	}
	
	public int totalHouses() {
		int houses=0;
		for (int i=0; i<properties.size(); i++) {
			if (properties.get(i) instanceof Territory) {
				houses += ((Territory) properties.get(i)).getHouses();
			}
		}
		return houses;
	}
	
	public int propertyValue() {
		int total=0;
		for (int i=0; i<properties.size(); i++) {
			total+=properties.get(i).getPrice();
			if (properties.get(i) instanceof Territory) {
				Territory field = (Territory) properties.get(i);
				total+=field.getHousePrice()*field.getHouses();
			}
		}
		return total;
	}
	
	public int buildingsWithColor(String color) {
		int builds=0;
		for (int i=0; i<properties.size(); i++) {
			if (properties.get(i) instanceof Territory) {
				if (((Territory) properties.get(i)).getColor().equals(color)) builds++;
			}
		}
		return builds;
	}
	
	public int buildingsInGroup(int fieldnumber) {
		int builds=0;
		for (int i=0; i<properties.size(); i++) {
			if (properties.get(i).getNumber()==fieldnumber) builds = ((Territory) properties.get(i)).getPropertiesInGroup();
		}
		return builds;
	}
	
	public int getPropertyIndex(int fieldnumber) {
		int index = 0;
		for (int i=0; i<properties.size(); i++) {
			if (properties.get(i).getNumber()==fieldnumber) index = i;
		}
		return index;
	}
}
